/*
 * AALActorItem.c
 *
 *  Created on: 10.06.2011
 *      Author: sili
 */

#include "AALActorItem.h"

static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("at.sili.aalsystem.model.AALActorItem"));

AALActorItem::AALActorItem(const int _id, Controller * _controller)
			 : AALSensorItem(_id,_controller){
	AAL_TRACE(logger,"Constructor globalId:%d", AALItem::globalId);
	this->adjustedValue = 0;
	this->type.clear();
	this->type.append("actuator");
}

AALActorItem::~AALActorItem() {
	AAL_TRACE(logger,"Destructor globalId:%d", AALItem::globalId);
}

void AALActorItem::toDOMElementParameters(DOMElement * _selfNode){
	// default stuff
	AALSensorItem::toDOMElementParameters(_selfNode);

	// my additional stuff
	const int length = 100;
	char integerBuffer[length];
	XMLCh tempStr0[length];
	XMLCh tempStr1[length];

	snprintf(integerBuffer, (size_t)length, "%d", this->adjustedValue);
	XMLString::transcode("adjustedValue", tempStr0, length-1);
	XMLString::transcode(integerBuffer, tempStr1, length-1);
	_selfNode->setAttribute(tempStr0,tempStr1);
}

void AALActorItem::fromDOMElementParameters(DOMElement * _selfNode, const char * _namespace){
	// default stuff
	AALSensorItem::fromDOMElementParameters(_selfNode, _namespace);

	// my additional stuff
	string parameter = _namespace;
	parameter += "adjustedValue";
	this->adjustedValue = Tools::getIntAttributeValue(_selfNode, parameter.c_str());
}

void AALActorItem::visualizeSelf(Visualizer * _visualizer, AALAction * _action){

	Template * cp = this->controller->getTemplateFactory()->getTemplate(this->templateName.c_str());

	if( _action->type == doInfo ||
		_action->type == doWarn ||
		_action->type == doAlarm){
		AALItem::visualizeSelf(_visualizer, _action);
		return;
	}

	// Parent does the default stuff
	AALSensorItem::visualizeSelf(_visualizer, _action);

	// Add adjust value
	const cFont * font = cFont::GetFont(fontOsd);
	if(_visualizer->getInfoPane() != NULL){
		char integerBuffer[10];
		snprintf(integerBuffer, (size_t)9, "%d", this->adjustedValue);

		string textline("");

		if(cp != NULL){
			// check if this actuator is boolean (on/of) or continuous
			if(abs(cp->maxValue - cp->minValue) > 1){
				// CONTINUOUS
				textline.append(tr("Voreingestellter Wert:"));
				textline.append(" ");
				textline.append(integerBuffer);
				textline.append(" ");
				textline.append(cp->unit.c_str());
			}
		} else {
			textline.append(tr("Voreingestellter Wert:"));
			textline.append(" ");
			textline.append(integerBuffer);
		}
		this->textArea.top =this->textArea.top + this->textArea.height;
		_visualizer->getInfoPane()->DrawText(this->textArea, textline.c_str(), _visualizer->getTextColor(), clrTransparent, font);
	}

	if(_visualizer->getControlPane() != NULL){

		if(cp != NULL){
			// check if this actuator is boolean (on/of) or continuous
			if(abs(cp->maxValue - cp->minValue) == 1){
				// BOOLEAN (ON/OFF)
				if(this->currentValue == cp->minValue)
					_visualizer->getControlPane()->drawControl(1, cp->switchOnName.c_str(), 0xF0000000, clrGreen, font, DrawPane::taCenter);
				else if(this->currentValue == cp->maxValue)
					_visualizer->getControlPane()->drawControl(1, cp->switchOffName.c_str(), 0xF0000000, clrGreen, font, DrawPane::taCenter);
			} else {
				// CONTINUOUS

				// DECREASE
				if(this->adjustedValue - cp->step >= cp->minValue)
					_visualizer->getControlPane()->drawControl(1, cp->decreaseName.c_str(), 0xF0000000, clrGreen, font, DrawPane::taCenter);
				else
					_visualizer->getControlPane()->drawControl(1, "", 0xF0000000, clrGreen, font, DrawPane::taCenter);

				// INCREASE
				if(this->adjustedValue + cp->step <= cp->maxValue)
					_visualizer->getControlPane()->drawControl(2, cp->increaseName.c_str(), 0xF0000000, clrYellow, font, DrawPane::taCenter);
				else
					_visualizer->getControlPane()->drawControl(2, "", 0xF0000000, clrYellow, font, DrawPane::taCenter);
			}
		}
	}
}

eOSState AALActorItem::guiAction(eKeys _key){
	if(_key == kGreen || _key == kYellow){
		Template * cp = this->controller->getTemplateFactory()->getTemplate(this->templateName.c_str());
		if(cp != NULL){
			char newValue[10];
			if(_key == kGreen && abs(cp->maxValue - cp->minValue) == 1){
				// BOOLEAN (ON/OFF)
				this->adjustedValue = abs(1-this->adjustedValue);
			} else {
				// CONTINUOUS
				if(_key == kYellow && this->adjustedValue + cp->step <= cp->maxValue){
					this->adjustedValue += cp->step;
				}

				if(_key == kGreen && this->adjustedValue - cp->step >= cp->minValue){
					this->adjustedValue -= cp->step;
				}
			}
			snprintf(newValue, (size_t)9, "%d", this->adjustedValue);
			this->controller->publish(doSetValue,newValue,this);
			return osUser1;
		}
	}
	return osContinue;
}

